-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Support non-universal impl
blocks
#19372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rust: Support non-universal impl
blocks
#19372
Conversation
c66a71a
to
a9afbb3
Compare
76baa34
to
02115f6
Compare
…rait implementation
439b202
to
68860b1
Compare
68860b1
to
a545361
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results on Rust LGTM - fewer consistency check failures, DCA looks good, except there appears to be an analysis time slowdown. It might be worth checking what's causing that, whether it affects other languages, and whether it's easy to fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First batch of review comments, mostly concerning performance.
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
d55edd7
to
0cf60c4
Compare
* `potentialInstantiationOf`. Defaults to simply projecting the third | ||
* argument of `potentialInstantiationOf`. | ||
*/ | ||
default predicate relevantTypeMention(TypeMention tm) { potentialInstantiationOf(_, _, tm) } |
Check warning
Code scanning / CodeQL
Missing QLDoc for parameter Warning
I think the PR is now ready for another look :) DCA shows a big slowdown on |
This fixes a test failure where duplicated functions from extraction caused a bunch of spurious results to pop up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.
a2436ab
to
eb5d4ef
Compare
Adds type inference support for non-universal
impl
blocks. By "non-universal" we meanimpl
blocks that target generic types but which are not valid for all instantiations of the generic type.For instance
where the
flatten
method is only valid for someOption
instantiations.Non-universal
impl
block affect both method resolution and trait implementations as the method/trait implementation can be valid only for some instantiations of a type. AFoo<i64>
might have a different set of methods/traits than aFoo<String>
. Finding the right method/trait implementation is the crux of the matter. The tests have examples of this.I've tried to document the new additions in this PR with QLdoc, but here are some additional high-level comments on the changes:
As mentioned above, with non-universal
impl
blocks it is no longer enough to know the root of a type to determine which traits it implements and which methods it supports. This affects a bunch of things.getMethod
andgetABaseTypeMention
member predicate onType
is removed, as aType
is now not enough to determine these things.conditionSatisfiesConstraint
takes aTypeMention
as its second parameter as aType
is not enough.In this PR I've split subtype handling (inferring type parameters through supertypes) from constraint handling (inferring type parameters from type parameter interface constraints (in C#)/trait bounds (in Rust). The former is now the sole job of the
AccessBaseType
sub-module and the later is done in the newAccessConstraint
sub-module. There's also a predicate for each in the module signature:getABaseTypeMention
andconditionSatisfiesConstraint
.This has both pros and cons:
getABaseTypeMention
asnone()
and avoid any computation related to subtyping.All in all I'm not sure which approach is best, but when I made this change performance improved quite a bit (but that was before making some other optimizations) so that's why I ended up with this. Another approach (in follow up work) could be to 1/ merge the two things back again, 2/ add a
getVariance
predicate for access positions than can becovariant
orinvariant
, 3/ only do subtyping for covariant positions, 4/ make all positions invariant for Rust. That should give equal performance for Rust, cut down on the duplicated code and hopefully the optimization withcountConstraintImplementations
would mean that subtyping for languages like C# wouldn't regress in performance (but we'd have no way to measure that).